package net.jueb.frameWorkDemo.mvc4mod.mapper;

import java.util.List;
import java.util.Set;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import net.jueb.frameWorkDemo.mvc4mod.bean.Account;

public interface AccountMapper {

	public final static String TABLE_NAME="t_account";
	
	@Options(useGeneratedKeys=true,keyProperty="id")
    @Insert("insert into "+TABLE_NAME+"("
    		+ "id,thirdOrderId,roleId,pn,shopItemId,rmb,money,giftMoney,diamond,giftDiamond,monthCardDays,createTime,state,stateTime"
    		+ ") "
    		+ "values("
    		+ "#{id},#{thirdOrderId},#{roleId},#{pn},#{shopItemId},#{rmb},#{money},#{giftMoney},#{diamond},#{giftDiamond},#{monthCardDays},#{createTime},#{state},#{stateTime}"
    		+ ")")  
    public int insert(Account role);  
      
    @Update("update "+TABLE_NAME+" set thirdOrderId=#{thirdOrderId},roleId = #{roleId},pn = #{pn}, "
    		+ "shopItemId = #{shopItemId}, rmb = #{rmb}, money = #{money},diamond = #{diamond},giftMoney = #{giftMoney},giftDiamond = #{giftDiamond},"
    		+ "monthCardDays = #{monthCardDays},createTime = #{createTime}, state = #{state},stateTime = #{stateTime}"
    		+" where id = #{id}")  
    public int update(Account order);
      
    @Select("select * from "+TABLE_NAME+" where id = #{id}")
    public Account findById(String id); 
    
    @Select("select * from "+TABLE_NAME+" where roleId = #{roleId}")
    public List<Account> findByRoleId(long roleId); 
    
    @Select("select * from "+TABLE_NAME+" where roleId = #{roleId} and shopItemId = #{shopItemId}")
    public List<Account> findByRoleIdItemId(@Param("roleId")long roleId,@Param("shopItemId")int shopItemId); 
    
    /**
     * 购买记录
     * @param roleId
     * @return
     */
    @Select("select roleId,shopItemId,COUNT(shopItemId) as count from "+TABLE_NAME+" where roleId = #{roleId} and state in('Payed','Settlement') GROUP BY roleId,shopItemId")
    public List<Account> findOrderCount(long roleId); 
    
    /**
     * 赠送金币的购买记录
     * @param roleId
     * @return
     */
    @Select("select roleId,shopItemId,COUNT(shopItemId) as count from "+TABLE_NAME+" where roleId = #{roleId} and state in('Payed','Settlement') and giftMoney>0 GROUP BY shopItemId")
    public List<Account> findOrderMoneyGiftCount(long roleId);
    /**
     * 赠送钻石的购买记录
     * @param roleId
     * @return
     */
    @Select("select roleId,shopItemId,COUNT(shopItemId) as count from "+TABLE_NAME+" where roleId = #{roleId} and state in('Payed','Settlement') and giftDiamond>0 GROUP BY shopItemId")
    public List<Account> findOrderDiamondGiftCount(long roleId); 
    
    /**
     * 查询指定物品购买赠送记录
     * @param roleId
     * @param shopItemId
     * @return
     */
    @Select("select id from "+TABLE_NAME+" where roleId = #{roleId} and shopItemId = #{shopItemId} and state in('Payed','Settlement') and giftMoney>0")
    public Set<String> findOrderMoneyGiftIds(@Param("roleId")long roleId,@Param("shopItemId")int shopItemId);
    /**
     * 查询指定物品购买赠送记录
     * @param roleId
     * @param shopItemId
     * @return
     */
    @Select("select id from "+TABLE_NAME+" where roleId = #{roleId} and shopItemId = #{shopItemId} and state in('Payed','Settlement') and giftDiamond>0")
    public Set<String> findOrderDiamondGiftIds(@Param("roleId")long roleId,@Param("shopItemId")int shopItemId); 
      
    @Select("select * from "+TABLE_NAME+"")  
    public List<Account> findAll(); 
}
